﻿/*!
 * 下拉框控件.
 * ecp.qzz.ui.input.combobox.js
 * 创建：QZZ
 * 日期：2014-03-01
 */
define(["qzz.iedit","qzz.ptreeview"], function() {
	
	var qzz = window.qzz;
	qzz.space("ui.input");
	/**
	 * 下拉框控件.
	 */
	qzz.ui.input.combotree = qzz.Class(qzz.ui.input.edit, {
		/**
		 * 创建函数.
		 */
		_create:function() {
			this.base();
		    this.className = "ComboTree";
		    this.logInfo("select.create");
		    //下拉按钮
		    this.button = this.createElement("span");

		    this.caret = this.createElement("span");
		    
		    this.keySelect = true;
		    
		    this.changeState = true;
		    this._valText = null;
		    
	    	this.multiSelect = this.getBooleanValue(this.getOption().multiple, false);
	    	this.getOption().multiSelect = this.getBooleanValue(this.getOption().multiSelect, this.multiSelect);
	    	//this.getOption().selectAll = this.getBooleanValue(this.getOption().selectAll, false);
	    	this.getOption().search = this.getBooleanValue(this.getOption().search, false);
	    	this.getOption().more = this.getBooleanValue(this.getOption().more, false);
		},
		/**
		 * 渲染.
		 */
		_render:function() {
			this.base();	
		    //下拉列表		    
	    	this.logInfo("select.render");
	    	this.setStyle(this.button, "componet_ui_input_dropBotton");
	    	//倒三角
	    	this.setStyle(this.caret, "dropButton_caret");
	    	this.button.appendChild(this.caret);
	    	var _this = this;
	    	if(this.getOption().data != null) {
	    		var dataList = this.getOption().data;
	    		this._value = [];
	    		this._text = [];
	    		for(var i = 0,len = dataList.length; i < len; i++) {
	    			if(dataList[i].checked) {
	    				this._value.push(dataList[i][this.getOption().idkey]);
	    				this._text.push(dataList[i][this.getOption().capkey]);
	    			}
	    		}
	    		if(this.getOption().multiSelect === false && this._value.length == 0) {
	    			this._value = "";
	    			this._text = "";
	    		}
	    	}
	    	if(this.getOption().width != null && !/\d+%$/.test(this.getOption().width)) {
	    		this.setWidth(parseInt(this.getOption().width, 10));
	    	}
	    	//输入事件
	    	this.inputBox.onkeydown = this.doInput;	   
	    	this.inputBox.onkeyup = this.doInputerKeyUp;
	    	//按钮事件.
	    	this.button.onclick = function() {
	    		if(_this._enabled && _this.getOption().readOnly !== true) {
		    		_this.hasSelect = true;
		    		_this._doDropList();
		    		_this.selectState = true;
	    		}
	    	}
	    	//初始化数据
	    	if(this._value && this._value.length !=0){
	    		this.initData = $.extend(true,[],this.getOption().data);
	    		var re = {};
	    		re[this.getOption().idkey] = this._value;
	    		re[this.getOption().capkey] = this._text;
	    		this.setValue(re);
	    	}
		},
		/**
		 * 获取数据.
		 */
		getInitData:function() {
            if(this.isEmpty(this.option.data) && this.hasInitSys != true) {
            	var ds = this.initSysData();
            	this.hasInitSys = true;
            	if(this.isArray(ds)) {
                    this.option.data = ds;
            	}
            }
            var re = this.option.data;
            var nd = this.option.needData;
            if(this.isArray(nd) && nd.length > 0) {
                if(this.option.data != null) {
                    re = nd.concat(this.option.data);
                } else {
            	    re = nd;
                }
            }
            return re;
        },
        /**
         * 初始化数据.
         * @name value
         */
        value:function(val) {
            if(val == null) {
            	var rev = this.getValue();
            	//全部
            	if(this.selectAll && rev && rev[0] === "all"){
            		var d = this.getData();
            		rev = [];
            		for(var i=0,j=d.length; i<j; i++){
            			if(i===0 && d[i][this.getOption().idkey]=="all"){continue;}
            			if(d[i].checked){
            				rev[rev.length] = d[i][this.getOption().idkey];
            			}
            		}
            	}
            	if(this.isArray(rev)) {
            		rev = rev.join(",");
            	}
                return rev;
            } else {
                var text = "";                
                if(this.getOption().multiSelect) {
                	val = val.split(/[ ,，]+/g)
                }
                var nd = this.option.needData;
                //处理默认选项中是否包含
                if(!this.getOption().multiSelect && this.isArray(nd) && nd.length > 0) {
                	for(var j = 0, len = nd.length; j < len; j++) {
                		if(nd[j][this.getOption().idkey] == val) {
                			text = nd[j][this.getOption().capkey];
                			break;
                		}
                	}
                	if(text !== "") {
                		var re = {};
                		re[this.getOption().idkey] = val;
                		re[this.getOption().capkey] = text;
                		this.base(re);
                		return;
                	}
                }
                //获取所有数据来进行查找
                var data = this.getInitData();
                if(this.isArray(data)) {
	                var len = data.length;
	                if(this.isArray(val)) {
	                	text = [];
	                	for(var i = 0, vlen = val.length; i < vlen; i++) {
	                		for(var j = 0; j < len; j++) {
	                			if(data[j][this.getOption().idkey] == val[i]) {
	                				text.push(data[i][this.getOption().capkey]);
	                				break;
	                			}
	                		}
	                	}
	                } else {
		                for(var i = 0; i < len; i++) {
		                  if(data[i][this.getOption().idkey] == val) {
		                      text = data[i][this.getOption().capkey];
		                      break;
		                  }      
		                }
	                }
                }
                var re = {};
                re[this.getOption().idkey] = val;
                re[this.getOption().capkey] = text;
                this.base(re);
            }
        },
	    /**
	     * 设置/读取文本内容.
	     * @name text
	     * @param {string} txt 字符串
	     * @return 返回文本内容
	     */
	    text:function(txt) {
	    	if(txt == null) {
	    		var rev = this.getText();
            	if(typeof rev === "number") {
            		rev = rev + "";
            	} else if(this.isArray(rev)) {
            		rev = rev.join(",");
            	}
                return rev;
	    	} else {
	    		this.setText(txt);
	    	}
	    },
        /**
         * 初始化服务端数据.
         * @name initSysData
         */
        initSysData :function() {
            var data = this.option.data;
            if(this.isNotEmpty(this.dataField) &&( this.isEmpty(data) || data.length <= 0)) {
                    var metaItem = this.getDataSource().getMetaDataModel().get(this.dataField).value();
                    var dataType = metaItem.dataType;
                    if(dataType == "enum" || dataType === "sysenum") {
                    	//添加枚举处理
        	    		var rtv = $.ecp.utils.getRefTypeValues(metaItem);
        	    		//获取名称转换列表
                        var displayMD = this.getDataSource().getDisplayModel();
            			inJson = displayMD.displayModel[rtv[0].classId.toUpperCase()];
            			if(this.isEmpty(inJson) || this.isEmpty(inJson[rtv[0].typeId])) {
            			    displayMD.getMc({refTypeValues : rtv, id: "1"});
            			    inJson = displayMD.displayModel[rtv[0].classId.toUpperCase()];
            			}
            			if(this.isNotEmpty(inJson) && this.isNotEmpty(inJson[rtv[0].typeId])) {
        			            inJson = inJson[rtv[0].typeId];
            			}
                        data = [];
            			//名称转换列表初始化
            			if(this.isNotEmpty(inJson)) {
            			    if(this.isArray(inJson)) {
            			        for(var i = 0, len = inJson.length; i < len; i++) {
            			            var textStr = inJson[i];
            			            if(typeof textStr == "object") {
            			                textStr = textStr.name;
            			            }
            			    	    if(this.isNotEmpty(key)) {
            			    	    	var re = {};
            			    	    	re[this.getOption().idkey] = i;
            			    	    	re[this.getOption().capkey] = textStr;
                                        data.push(re);
            			    	    }
            			        }
            			    } else {
            			        for(var key in inJson) {
            			    	    var textStr = inJson[key];
            			    	    if(typeof textStr == "object") {
            			    		textStr = textStr.name;
            			    	    }
            			    	    if(this.isNotEmpty(key)) {
            			    	    	len = key.length;
            			    	    	if(!isNaN(key) && (len == 1
            			    	    		|| key.length > 1 && key.chatAt(0) !== "0")) {
            			    	    		key = +key;
            			    	    	}
            			    	    	var re = {};
            			    	    	re[this.getOption().idkey] = i;
            			    	    	re[this.getOption().capkey] = textStr;
                                        data.push(re);
            			    	   }
                                                
            			        }
          			        }
            			}
            			inJson = null;
            			rtv = null;
                    }
		
              }
              return data;
        },
	    /**
	     * 执行失去焦点事件.
	     * @param ev 事件对象.
	     */
	    doBlur:function(ev){
	    	if(this.popTreeView != null) {
	    		var hasSelect = this.popTreeView.hasSelect;
	    		this.popTreeView._docMouseUp();
	    		this.popTreeView.hasSelect = hasSelect;
	    	}
	    	if(this.popTreeView == null || !this.popTreeView.isFocus()) {
	    	    this.base();
	    	}
	    },
		/**
		 * 创建下拉对象.
		 * @name _createPopObject
		 * @ignore
		 */
		_createPopObject:function() {
			if(this.isEmpty(this.popTreeView)) {
				//初始化数据
                var data = this.getInitData();
                //
				this.popTreeView = 
					new qzz.ui.drop.poptreeview(undefined, 
						    {"cssPrefix":this._cssPrefix,
							 "data":data,
							 "selectAll":this.getOption().selectAll,
							 "search":this.getOption().search,
							 "more":this.getOption().more,
							 "multiSelect":this.getOption().multiSelect, 
							 "fontSize":this.getOption().fontSize,
							 "fullPath":this.getOption().fullPath,
							 "idkey":this.getOption().idkey,
							 "capkey":this.getOption().capkey,
							 "radius":this.getOption().radius,
				    		 "checkFlag":this.getOption().checkFlag,
				    		 "unCheckFlag":this.getOption().unCheckFlag,
				    		 "leafOnly":this.getOption().leafOnly
						   });
				var _this = this;
				//选择复选择框的时候
				this.popTreeView.bind("onCheckChanged", function(node, state){
	    			var selList = this.getSelectedData();
	    			itemid = [];
	    			itemtext = [];
	    			//全部
	    			if(_this.selectAll){
	    				if(_this.selectAll && _itemid == "all" && selList.length > 0){
		    				itemid[0] = _itemid;
		    				itemtext[0] = _itemtext;
		    			}else {
		    				var list = this.listView.list.getItemList();
							var count = 0;
							for(var i=0,j=list.length; i<j; i++){
								if(list[i].disabled && !list[i].checked){continue;}
								count ++;
							}
		    				if(selList.length != count){
			    				for(var i = 0, len = selList.length; i < len; i++) {
			    					if( i === 0 && selList[i][_this.getOption().idkey] === "all"){continue;}
			    					var id = selList[i][_this.getOption().idkey];
			    					if(id == null) {
			    						id = selList[i][_this.getOption().itemCode];
			    					}
				    				itemid[i] = id;
				    				itemtext[i] = selList[i][_this.getOption().capkey];
				    			}
	    					}else{
	    						itemid[0] = "all";
			    				itemtext[0] = "("+this.getI18n("qzz.all", "全部")+")";
	    					}
		    			}
	    			} else {
	    				for(var i = 0, len = selList.length; i < len; i++) {
	    					var id = selList[i][_this.getOption().idkey];
	    					if(id == null) {
	    						id = selList[i][_this.getOption().itemCode];
	    					}
		    				itemid[i] = id;
		    				itemtext[i] = selList[i][_this.getOption().capkey];
		    			}
	    			}
	    			_this._value = itemid;
	    			_this._text = itemtext;
	    			_this.refresh();
				});				
				//选择事件
		    	this.popTreeView.onSelect = function(node) {
		    		if(_this.getOption().multiSelect) {
		    			return;
		    		}
		    		var itemid = this.getValue(node);
		    		if(itemid == null) {
		    			itemid = this.getDisplay(node);
		    		}
		    		var itemtext = this.getDisplay(node);
		    		var _itemid = itemid;
		    		var _itemtext = itemtext;    		
		    		var re = {};
		    		re[_this.getOption().idkey] = itemid;
		    		re[_this.getOption().capkey] = itemtext;
		            _this.setValue(re);
		            //_this.popTreeView.clearFilter();
					_this.popTreeView.hide();
		            _this.setFocus();
		            if(typeof _this.onSelect == "function") {
		            	try {
		            	    _this.onSelect(node);
		            	} catch(e) {
		            		_this.logInfo("[onSelect]ERR:" + e.message);
		            	}	            	    
		            }
		            _this.trigger("onSelect", [node]);
		            _this.inputBox.focus();
		            _this.setPosition(_this.inputBox, _this.inputBox.value.length);	            
		        }
		    	//隐藏事件
		    	this.popTreeView.onHide = function() {
		    		_this._popState = false;
		    		if(typeof _this.onHide == "function") {
		            	try {
		            	    _this.onHide();
		            	} catch(e) {
		            		_this.logInfo("[onHide]ERR:" + e.message);
		            	}
		            }
		    	}
		    	this.popTreeView.bind("onMoreClick", function() {
		    		_this.trigger("onMoreClick",[_this]);
		    	});
		    	this.popTreeView.setParent(this.getModelBody());
			} else if(this.changeState) {
				//初始化数据
                var data = this.getInitData();
		    	this.popTreeView.setItemList(data);
			}
			this.changeState = false;
		},
		refreash:function() {
			this.refresh();
		},
		/**
		 * 刷新.
		 * @name refresh
		 * @example
		 *     cob.refresh();
		 */
		refresh:function(){
	    	this.base();
	    	var _this = this;
	    	this.inputBox.style.width = (100 - 21/this.getWidth()) + "%";
	    	this.thisWindow.appendChild(this.button);
	    	this.button.appendChild(this.caret);
	    	this.caret.style.top = (this.getHeight()/2 - 2) + "px";
	    	if(this._readOnly !== true) {		    	
		    	this.inputBox.onclick = function(ev) {
		    		var ev = ev || event;
		    		if(typeof _this.onEditClick == "function") {
		    		    _this.onEditClick(ev);
		    		}
		    	};
	    	} else {	    		
	    		this.inputBox.onclick = function() {
		    		if(_this._enabled) {
			    		//if(_this.popTreeView != null) {
			    		//    _this.popTreeView.clearFilter();
			    		//}
			    		_this.hasSelect = true;
			    		_this._doDropList();
			    		_this.selectState = true;
		    		}
	    		}
	    	}
	    },
	    /**
	     * 设置宽度.
	     * @param value 宽
	     */
	    setWidth:function(value) {
	    	this.base(value);
	    	if(value < 150) {
	    		value = 150;
	    	}
	    },
	    setSearch:function(search) {
	    	this.getOption().search = this.getBooleanValue(search, false);
	    },
	    setMore:function(more) {
	    	this.getOption().more = this.getBooleanValue(more, false);
	    },
	    /**
	     * 执行按键弹起.
	     * @param ev 事件对象
	     * @param key 键值
	     */
	    doKeyUp:function(ev, key) {
	    	this.base(ev, key);
	    	if(this.getBrowser() !== "IE") {
		    	this.inputBox.focus();
		    	if(key === this.keyBoard.UP) {
		    		//设置鼠标位置
		    	    this.setPosition(this.inputBox, this.inputBox.value.length);
				} else if(key === this.keyBoard.ENTER) {
					//if(this.popTreeView.isShow()) {
					//	this.popTreeView.hide();
					//}
					if(this.popTreeView != null && !this.popTreeView.isShow()) {
					    this.postValue();
					}
				}
			}
	    },
	    /**
	     * 执行按键按下.
	     * @param {object} ev 事件对象
	     * @param {number} key 键值
		 * @ignore
	     */
	    doKeyDown:function(ev, key) {
	    	 //调用基类函数
	    	this.base(ev, key)
	    	this._createPopObject();
	    	if(this.popTreeView.isShow()) return;
	    	if(key === this.keyBoard.F2) {
//	    		var citem = this.popTreeView.getItem();
//	    		if(citem == null || citem[this.getOption().idkey] != this._value) {
//	    			this.popTreeView.setSelect(this._value);
//	    		}
	    		//按键弹出
	    		this.button.onclick();
	    	} else if(this.keySelect && key === this.keyBoard.UP && !this.getOption().multiSelect) {
	    		var citem = this.popTreeView.getItem();
	    		if(citem == null || citem[this.getOption().idkey] != this._value) {
	    			this.popTreeView.setSelect(this._value);
	    		}
	    		//上移
	    		this.popTreeView.prior();  
	    		var item = this.popTreeView.getItem();
	    		if(item != null) {
	    			var itemid = item[this.getOption().idkey];
		    		if(itemid == null) {
		    			itemid = item[this.getOption().capkey];
		    		}
		    		var re = {};
		    		re[_this.getOption().idkey] = itemid;
		    		re[_this.getOption().capkey] = item[_this.getOption().capkey];
			        this.setValue(re);
		    	}
	    		item = null;
	    	} else if(this.keySelect && key === this.keyBoard.DOWN && !this.getOption().multiSelect) {
	    		var citem = this.popTreeView.getItem();
	    		if(citem == null || citem[this.getOption().idkey] != this._value) {
	    			this.popTreeView.setSelect(this._value);
	    		}
	    		//下移
	    		this.popTreeView.next();
	    		var item = this.popTreeView.getItem();
	    		if(item != null) {
	    			var itemid = item[this.getOption().idkey];
		    		if(itemid == null) {
		    			itemid = item[this.getOption().capkey];
		    		}
		    		var re = {};
		    		re[this.getOption().idkey] = itemid;
		    		re[this.getOption().capkey] = item[this.getOption().capkey];
			        this.setValue(re);
		    	}
	    		item = null;
	    	} else if(key === this.keyBoard.ENTER) {
	    		//回车
	    		if(!this.popTreeView.isShow()) {
	    			this.popTreeView.setBlur();
	    		    this.button.onclick();
	    		} else {
	    			if(this.popTreeView.selectIndex < 0) {
	    				this.popTreeView.hide();
	    			}
	    		}
	    	}
	    },
	    /**
	     * 提交数据.
		 * @name postValue
		 * @ignore
	     */
	    postValue:function() {
	    	this._createPopObject();
	    	var txtValue = this.inputBox.value;
	    	if(this.notnull === true && txtValue === "") {
	    		return;
	    	}
	    	if(typeof txtValue == "string") {
				txtValue = this._tagToText(txtValue);
			}
	    	if(txtValue.indexOf(",") < 0) {
	    		if(txtValue != this._text || this.getOption().multiSelect != true) {
		    		var ri = this.popTreeView.indexOfText(txtValue);
			    	if(ri == null) {
			    		if(this._readOnly == true && txtValue !== "") {
			    		    this.inputBox.value = this._text;
			    		} else {
		    				var datas = this.initData;
			    			var _val = [],_text=[];
			    			if(datas){
			    				for(var i=0,j=datas.length; i<j; i++){
				    				if(datas[i].disabled && datas[i].checked){
				    					_val.push(datas[i][this.getOption().idkey]);
				    					_text.push(datas[i][this.getOption().capkey])
				    				}
				    			}
			    			}
			    			re = {};
			    			if(_val.length === 0){
			    				re[this.getOption().idkey] = txtValue;
			    				re[this.getOption().capkey] = txtValue;
			    				this.setValue(re);
			    			} else {
			    				re[this.getOption().idkey] = _val;
			    				re[this.getOption().capkey] = _text;
			    				this.setValue(re);
			    			}
			    		}
			    	} else {
			    		var item = ri.data;
			    		var itemid = item[this.getOption().idkey];
			    		if(itemid == null) {
			    			itemid = item[this.getOption().capkey];
			    		}
			    		var re = {};
			    		re[this.getOption().idkey] = itemid;
			    		re[this.getOption().capkey] = item[this.getOption().capkey];
			    		if(this.getOption().fullPath !== true || this.getText() !== re[this.getOption().capkey]) {
			    		    this.setValue(re);
			    		}
			    		item = null;
			    	}
	    		}
	    	} else {
	    		var tv = txtValue;
	    		if(this.isArray(tv)) {
	    			tv = tv.join(",");
	    		}
	    		if(this.getOption().multiSelect != true || this._text != tv) {
	    			var _this = this;
		    		this.popTreeView.setSelectByText(txtValue, function(selItems){
		    			var val = [], txt = [];
				    	for(var i = 0, len = selItems.length; i < len; i++) {
				    		var id = selItems[i][_this.getOption().idkey];
				    		if(id == null) {
				    			id = selItems[i][_this.getOption().capkey];
				    		}
				    		val.push(id);
				    		txt.push(selItems[i][_this.getOption().capkey]);
				    	}
				    	var re = {};
				    	re[_this.getOption().idkey] = val;
				    	re[_this.getOption().capkey] = txt;
				    	_this.setValue(re);
		    		});			    	
	    		}
	    	}
	    },
	    /**
	     * 通过val获取text值.
	     * @param {string|number} val 值
	     * @return {string} text 显示值
	     * @ignore
	     */
	    _getTextByValue:function(val) {
	    	if(val === "" || val == null) {
	    		return "";
	    	}
	    	if(this.isEmpty(this._valText)) {
	    		this._valText = {};
	    		var hasData = false;
	    		if(this.isArray(this.option.needData)) {
	    			var len = this.option.needData.length;
                    for(var i = 0; i < len; i++) {
                        var item = this.option.needData[i];
                        if(typeof item == "object") {
	                        if(item[this.getOption().idkey] != null) {
	                        	this._valText[item[this.getOption().idkey]] = item[this.getOption().capkey] || item[this.getOption().idkey];
	                        } else {
	                        	this._valText[item[this.getOption().capkey]] = item[this.getOption().capkey];
	                        }
                        } else if(item){
                        	this._valText[item] = item;
                        }
                    }
                    hasData = len > 0;
               }
	    	   if(this._valText[val] == null && this.isEmpty(this.option.data)) {
	    		   this.getData();
	    	   }
               if(this.isArray(this.option.data)) {
            	   var len = this.option.data.length
                   for(var i = 0; i < len; i++) {
                	   var item = this.option.data[i];
                	   if(typeof item == "object") {
	                	   if(item[this.getOption().idkey] != null) {
	                       	   this._valText[item[this.getOption().idkey]] = item[this.getOption().capkey] || item[this.getOption().idkey];
	                       } else {
	                       	   this._valText[item[this.getOption().capkey]] = item[this.getOption().capkey];
	                       }   
                	   } else {
                		   this._valText[item] = item;
                	   }
                   }
            	   hasData = hasData || len > 0;
               }
               if(hasData == false) {
            	   return val;
               }
	    	}
	    	if(this.getOption().fullPath === true) {
	    	    var valAry = val.split(".");
	    	    val = valAry[valAry.length - 1];
	    	}
	    	var res = this._valText[val];
	    	if(!res){
	    		res = val;
	    	}
	    	return res;
	    },
	    /**
	     * 输入控制.
	     * @param ev 事件对象
	     */
	    doInput:function(ev) {
	    	this.base(ev);
	    },
	    /**
	     * 执行下拉列表.
	     */
	    _doDropList:function() {
	    	this.hasInitSys = false;
	    	this._createPopObject();
    		if((this.option.data == null || this.option.data.length === 0)
                   && (this.option.needData == null || this.option.needData.length === 0)) {
    			return;
    		}
    		this.popTreeView.logInfo("click");
    		this.popTreeView.setWidth(this.getWidth());
    		//this.popTreeView.setHeight(300);
    		this.popTreeView.setSelectAll(this.selectAll);
    		var sels = this.getText();
    		if(!this.isArray(sels) && typeof sels !== "string") {
    			sels = sels + "";
    		}
    		if(this.getOption().multiSelect) {
    			var _this = this;
    		    this.popTreeView.clearSelectAll(function() {
    		    	_this.popTreeView.setSelectByText(sels);	
    		    });
    		} else {   		
    			if(this.isArray(sels)) {
    				sels = sels.join(",");
    			}
    		    this.popTreeView.setSelectByText(sels);
    		}
    		this.getAXY();
    		//显示下拉列表
    		this.popTreeView.execute(this.getATop() + this.getHeight() - 4, this.getALeft(), this.getHeight());
    		this._popState = true;
    		if(typeof this.onDrop == "function") {
    			try {
    				this.focus = false;
    			    this.onDrop(this);    			    
    			} catch(e) {
    			    this.logInfo("[onDrop]ERR:" + e.message);
    			}
    		}
	    },
	    /**
	     * 输入框按键弹起.
	     * @param ev 事件对象
	     */
	    doInputerKeyUp:function(ev) {
	    	var _this = this.ctrl;
	    	var ev = ev || window.event;
	    	var key = ev.keyCode || ev.charCode || ev.which;
	    	//过滤掉不需要的按键事件
	    	if(key === _this.keyBoard.ENTER 
	    			|| key === _this.keyBoard.UP
	    			    || key === _this.keyBoard.DOWN
	    			        || key === _this.keyBoard.LEFT
	    			            || key === _this.keyBoard.RIGHT
	    			               || key === _this.keyBoard.F2
	    			                  || key ===  _this.keyBoard.ESC
	    			                     || key === _this.keyBoard.TAB
	    			                        || ev.ctrlKey
	    			                          || key === _this.keyBoard.CTRL
	    			                             || _this._readOnly
	    	) {
	    		//取消控制按钮
	    		ev.returnValue = false;
	    		return false;	    		
	    	}
	    	_this._createPopObject();
	    	if(this.value === "") {
	    		_this.popTreeView.hide();
	    		//_this.popTreeView.clearFilter();
	    	} else {
	    		//_this.popTreeView.clearFilter();
	    		if(_this.getOption().multiSelect) {
	    			var fls = this.value.split(/[ ,，]+/g);
	    			var i = 0;
	    			for(var len = fls.length; i < len; i++) {
	    				if(fls[i] !== "") {
	    				    //_this.popTreeView.addFilter(fls[i], false);
	    				}
	    			}
	    			//_this.popTreeView.doFilter();	
	    			fls = null;
	    		} else {
	    		    //_this.popTreeView.addFilter(this.value);
	    		    _this.popTreeView.setSelectByText(_this.getText()+"");
	    		}
	    		if(!_this.popTreeView.isShow() && _this.popTreeView.length() > 0) {	    			
	    			_this._doDropList();
	    		} else if(_this.popTreeView.isShow() && _this.popTreeView.length() == 0) {
	    			_this.popTreeView.hide()
	    			_this.setFocus();
	    		}
	    	}
	    	
	    },
	    /**
	     * 设置列表.
	     * @param datas 数据
	     */
		setItemList:function(datas) {
			if(this.isArray(datas)) {
				var len = datas.length;
				if(len > 0 && typeof datas[0] != "object") {
					var ds = [];
					for(var i = 0; i < len; i++) {
						var re = {};
						re[this.getOption().idkey] = datas[i];
						re[this.getOption().capkey] = datas[i];
						ds.push(re);
					}
					datas = ds;
				}			
				this.option.data = datas;
			}
			if(this._value != null && this._value !== "" && this._text == "") {
                var data = this.getInitData();
                var item = null;
                for(var i = 0, len = data.length; i < len; i++) {
                    item = data[i];
                    if(item [this.getOption().idkey] != null && item [this.getOption().idkey] == this._value || item [this.getOption().idkey] == null && item [this.getOption().capkey] == this._value) {
                        this._text = item [this.getOption().capkey];
                        break;
                    }
                }
                this.refresh();
            }
            if(this.popTreeView != null && this.popTreeView.isShow()) {
                this.popTreeView.setItemList(this.getInitData());
            }            
			this.changeState = true;
			this.trigger("afterLoadData",[datas]);
			this._valText = null;			
		},
	    /**
	     * 加载外部数据.
	     * @param {Array} data 数据包
	     */
	    loadData:function(data) {
	    	this.setItemList(data);
	    },
	    /**
	     * 获取得数据.
	     * @name getData
	     * @return {Array} data返回数据包
	     */
	    getData:function() {
	    	return this.getInitData();
	    },
	    /**
	     * 设置为复选.
	     * @name setMultiSelect
	     * @param val 是否复选
	     */
	    setMultiSelect:function(val) {
	    	this.getOption().multiSelect = val;
	    },
	    setIdkey:function(key) {
        	this.base(key);
        	this._createPopObject();
        	this.popTreeView.setIdkey(key);
        }
	});
	
	/**
	 * 下拉编辑控件.
	 */
	_bindRenderFunc("qzzcombotree", qzz.ui.input.combotree);
	return qzz.ui.input.combotree;
});
	